package com.itcast.flink.sources.socket;

import com.itcast.flink.sources.socket.cache.GoodsCache;
import com.itcast.flink.sources.socket.pojo.Goods;
import com.itcast.flink.sources.socket.pojo.Order;
import com.itcast.flink.sources.socket.repository.OrderRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * <p>Description: </p>
 *
 * @author
 * @version 1.0
 * <p>Copyright:Copyright(c)2020</p>
 * @date
 */
@SpringBootApplication
@ComponentScan(basePackages = {"com.itcast"})
@EnableTransactionManagement
public class OrderSourceApplication {
    
    /**
     * 用户ID列表
     */
    private static Long[] userIdArr = new Long[]{10001L, 10002L, 10003L};
    
    private static Long[] goodsIdArr = new Long[]{1L, 2L, 3L, 4L, 5L};
    /**
     * 成交数量
     */
    private static Integer[] execVolumeArr = new Integer[]{1, 2, 3};
    
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(OrderSourceApplication.class, args);
        clearCache(applicationContext);
        while (true) {
            Random random = new Random();
            Order order = new Order();
            // 设置用户ID
            order.setUserId(userIdArr[random.nextInt(userIdArr.length)]);
            GoodsCache goodsCache = (GoodsCache) applicationContext.getBean("goodsCache");
            
            Long goodsId = goodsIdArr[random.nextInt(goodsIdArr.length)];
            Goods goods = goodsCache.get(goodsId);
            // 设置商品ID
            order.setGoodsId(goodsId);
            // 设置商品名称
            order.setGoodsName(goods.getGoodsName());
            // 设置成交价格
            order.setExecPrice(goods.getGoodsPrice());
            // 设置成交数量
            order.setExecVolume(execVolumeArr[random.nextInt(execVolumeArr.length)]);
            // 设置成交时间
            order.setExecTime(System.currentTimeMillis());
            OrderRepository orderRepository = applicationContext.getBean("orderRepository", OrderRepository.class);
            // 保存订单信息
            Order orderResult = orderRepository.save(order);
            System.out.println("orderId: " + orderResult.getId());
            // 缓存处理
            storeCache(orderResult, applicationContext);
            // 写入间隔时间
            Thread.sleep(1000);
        }
        
    }
    
    /**
     * 清除缓存
     *
     * @param applicationContext
     * @return
     */
    private static void clearCache(ConfigurableApplicationContext applicationContext) {
        CacheManager cacheManager = applicationContext.getBean("cacheManager", CacheManager.class);
        Cache cache = cacheManager.getCache("orderCache");
        cache.clear();
    }
    
    /**
     * 将订单ID保存至缓存
     *
     * @param orderResult
     * @param applicationContext
     */
    private static void storeCache(Order orderResult, ConfigurableApplicationContext applicationContext) {
        
        CacheManager cacheManager = applicationContext.getBean("cacheManager", CacheManager.class);
        Cache cache = cacheManager.getCache("orderCache");
        Cache.ValueWrapper orderWrapper = cache.get("orderIds");
        if (null != orderWrapper) {
            Set<String> orderIds = (Set<String>) orderWrapper.get();
            orderIds.add(orderResult.getId() + "," + orderResult.getUserId() + "," + orderResult.getExecTime());
            cache.put("orderIds", orderIds);
        } else {
            Set<String> orderIds = new HashSet<>();
            orderIds.add(orderResult.getId() + "," + orderResult.getUserId() + "," + orderResult.getExecTime());
            cache.put("orderIds", orderIds);
        }
        
    }
    
}
